package com.software.cool.api.admin.v1.controller;

import com.alibaba.fastjson.JSON;
import com.software.cool.entity.TbRole;
import com.software.cool.api.admin.v1.vo.AdminLoginUserVO;
import com.software.cool.api.admin.v1.vo.AdminV1Result;
import com.software.cool.entity.Page;
import com.software.cool.service.impl.TbRoleService;
import com.software.cool.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Api(value = "管理端-角色管理", tags = "管理端-角色管理")
@RestController
@Slf4j
@RequestMapping("/admin/v1/role")
@CrossOrigin
public class RoleController extends BaseController {
    @Resource
    private TbRoleService roleService;

    @ApiOperation(value = "分页", notes = "分页")
    @GetMapping("/page")
    public AdminV1Result list(String token,Integer page, Integer limit, String keyWord)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        AdminLoginUserVO loginUser= (AdminLoginUserVO) result.getData();
        TbRole role=new TbRole();
        if(CommonUtils.isNotBlank(keyWord)){
            role= JSON.parseObject(keyWord, TbRole.class);
        }
        Page pageVO=roleService.selectPage(page,limit,role);
        return  AdminV1Result.success(pageVO.getList(),pageVO.getTotalSize());
    }

    @ApiOperation(value = "列表", notes = "列表")
    @GetMapping("/list")
    public AdminV1Result list(String token,String keyWord)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        TbRole role=new TbRole();
        return  AdminV1Result.success(roleService.selectListBySQL(role));
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/detail")
    public AdminV1Result detail(String token,Long id)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        return  AdminV1Result.success(roleService.get(id));
    }

    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping("/add")
    public AdminV1Result add(@RequestBody TbRole model)throws Exception{
        AdminV1Result result=getAdmin(model.getToken());
        if(result.getCode()==-1){ return result; }
        roleService.edit(model);
        return AdminV1Result.success();
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/update")
    public AdminV1Result update(@RequestBody TbRole model)throws Exception{
        AdminV1Result result=getAdmin(model.getToken());
        if(result.getCode()==-1){ return result; }
        roleService.edit(model);
        return AdminV1Result.success();
    }

    @ApiOperation(value = "删除", notes = "删除")
    @GetMapping("/del")
    public AdminV1Result del(String token,Long id)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        TbRole model=roleService.get(id);
        if(model!=null){
            if(model.getRoleId().equals(1L))
            {
                return AdminV1Result.fail("超级管理员角色不能删除");
            }
            model.setUseStatus("deleted");
            roleService.update(model);
        }
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量添加", notes = "批量添加")
    @PostMapping("/addList")
    public AdminV1Result addList(String token,List<TbRole> list)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        roleService.addList(list);
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量修改", notes = "批量修改")
    @PostMapping("/updateList")
    public AdminV1Result updateList(String token,List<TbRole> list)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        roleService.updateList(list);
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量删除", notes = "批量删除")
    @GetMapping("/delList")
    public AdminV1Result delList(String token,String ids)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        String[] array=ids.split(",");
        if(array.length>0){
            for(int i=0;i<array.length;i++){
                TbRole model=roleService.get(CommonUtils.parseLongData(array[i]));
                if(model!=null){
                    if(model.getRoleId().equals(1L))
                    {
                        return AdminV1Result.fail("超级管理员角色不能删除");
                    }
                    model.setUseStatus("deleted");
                    roleService.update(model);
                }
            }
            return AdminV1Result.success();
        }
        return AdminV1Result.fail("删除失败");
    }

    @ApiOperation(value = "批量导入Excel", notes = "批量导入Excel")
    @PostMapping("/importData")
    public AdminV1Result importData(String token)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量导出Excel", notes = "批量导出Excel")
    @PostMapping("/exportData")
    public AdminV1Result exportData(String token)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        return AdminV1Result.success();
    }


}
